package com.sa35.fbs.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sa35.fbs.dao.UserDao;
import com.sa35.fbs.form.LoginForm;
import com.sa35.fbs.model.User;
import com.sa35.fbs.model.UserRoleEnum;
import com.sa35.fbs.service.FBSService;
import com.sa35.fbs.service.UserService;
import com.sa35.fbs.vo.UserVO;

@Service
public class UserServiceimpl extends FBSService implements UserService{
	
	@Autowired
	UserDao userDao;
	
	@Transactional
	@Override
	public UserVO auth(LoginForm loginForm){
		
		String userName = loginForm.getUserName();
		String userPwd = loginForm.getPassword();
		
		logger.info("auth for user: {}", userName);
		logger.info("auth for password: {}", userPwd);
		
		User user = userDao.findByUserName(userName);
		
		if(user!=null && userPwd.equals(user.getPassword())){
			UserRoleEnum role = user.getUserRole();
			logger.info("user role: {}", role.toString());
			return new UserVO(user.getId(),user.getUserName(),user.getUserRole());
		}else{
			logger.info("user not exist or password not match");
			return null;
		}
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
}
